<?php

declare (strict_types=1);

namespace app\admin\middleware;

use app\admin\service\AdminMenuService;
use Closure;
use think\response\Json;
use tmcore\interfaces\MiddlewareInterface;
use tmcore\traits\JsonTrait;

/**
 * 权限验证中间件
 */
class AuthMiddleware implements MiddlewareInterface
{
    use JsonTrait;

    /**
     * 权限验证
     * @param $request
     * @param Closure $next
     * @return mixed|Json
     */
    public function handle($request, Closure $next)
    {
        // 获取当前路由地址
        $url = strtolower(trim($request->rule()->getRule()));

        // 路由权限检验
        $res = (new AdminMenuService)->checkAuthRoute($url, $request->adminInfo);
        if($res) return $next($request);

        return self::error('您的权限不足，无法访问');
    }
}